import { Context } from "koa";
const { getUnivsById } = require('../service/univ.service')
const { invalidUnivId, idEmpty, verifyIdError } = require('../constants/err.type')

const univIdExist = async(ctx: Context, next: () => Promise<void>) => {
    try {
        const id = ctx.params.id
        if(!id) {
            return ctx.app.emit('error', idEmpty, ctx)
        }
        const course: any = await getUnivsById(id);
        if(!course) {
            return ctx.app.emit('error', invalidUnivId, ctx)
        }
    } catch (error) {
        console.error(error)
        return ctx.app.emit('error', verifyIdError, ctx)
    }
    await next()
}



module.exports = {
    univIdExist
}